package org.example.bishe1.controller;

import org.example.bishe1.pojo.Dept;
import org.example.bishe1.pojo.Result;
import org.example.bishe1.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/test")
    public Result test( String name, @RequestParam(required = false) Integer age){
        if (age == null) {
            return Result.success("age可以不带");
        } else {
            return Result.success("两个都要带");
        }
    }

    /*
    * 获取所有部门信息
     */
    @GetMapping("/depts")
    public Result getAllDepts(){
        List<Dept> deptList = deptService.getAllDepts();
        return Result.success(deptList);
    }

    /*
    删除部门by id
     */
    @DeleteMapping("/depts")
    public Result deleteDeptById(@RequestParam Integer id){
        deptService.deleteDeptById(id);
        return Result.success();
    }

    /*
    新增部门
     */
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return Result.success();
    }

    /*
    查询部门by id
     */
    @GetMapping("/depts/{id}")
    public Result getDeptById(@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    /*
    更新部门
     */
    @PutMapping("/depts")
    public Result updateDept(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return Result.success();
    }

}
